"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.menu.schemas.out import (
    MenuCompleteOutSchema,
    MenuSimpleOutSchema,
)
from src.business.admin.system.menu.schemas.validate import (
    MenuCreateValidateSchema,
    MenuDeleteValidateSchema,
    MenuListSearchValidateSchema,
    MenuUpdateStatusValidateSchema,
    MenuUpdateValidateSchema,
)
from src.business.admin.system.menu.service import MenuService
from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetUserNameFromAccessTokenDependencies,
)
from src.dependencies.no_introduction import depend_get_master_db_session


async def route_depend_get_menu(
    menu_id: Annotated[int, Annotated[int, Query(..., ge=1, description="菜单ID")]],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> MenuCompleteOutSchema:
    """查询单个菜单的路由依赖

    Args:
        menu_id (Annotated[int, Depends): 菜单ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        MenuCompleteOutSchema: 菜单信息
    """
    menu = await MenuService.get_menu_use_id(
        db_session,
        menu_id,
        raise_disabled_exception=False,
        raise_expired_exception=False,
    )
    return menu


async def route_depend_list_menu(
    search_schema: MenuListSearchValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> list[MenuCompleteOutSchema]:
    """查询菜单列表的路由依赖

    Args:
        search_schema (Annotated[MenuListSearchValidateSchema, Depends): 查询参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        list[MenuCompleteOutSchema]: 查询结果
    """
    # 列表结果查询
    search_result = await MenuService.list_search_complete_menu(
        db_session, search_schema
    )
    return search_result


async def route_depend_list_simple_menu(
    search_schema: MenuListSearchValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> list[MenuSimpleOutSchema]:
    """查询菜单简单信息列表的路由依赖

    Args:
        search_schema (Annotated[MenuListSearchValidateSchema, Depends): 查询参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        list[MenuSimpleOutSchema]: 查询结果
    """
    # 列表结果查询
    search_result = await MenuService.list_search_simple_menu(db_session, search_schema)
    return search_result


async def route_depend_create_menu(
    create_schema: MenuCreateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建菜单的路由依赖

    Args:
        create_schema (MenuCreateValidateSchema): 创建菜单所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        int: 创建实例对应的主键
    """
    # 创建操作
    created_id = await MenuService.create_menu(
        db_session, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_menu(
    update_schema: MenuUpdateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改菜单的路由依赖

    Args:
        update_schema (MenuUpdateValidateSchema): 修改菜单所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await MenuService.update_menu(db_session, update_schema, updater_username=username)


async def route_depend_update_menu_status(
    update_schema: MenuUpdateStatusValidateSchema,
    username: Annotated[
        str,
        Depends(GetUserNameFromAccessTokenDependencies.from_header),
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改菜单状态的路由依赖

    Args:
        update_schema (MenuUpdateStatusValidateSchema): 修改菜单所需的参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await MenuService.update_menu_status(
        db_session, update_schema, updater_username=username
    )


async def route_depend_delete_menu(
    delete_schema: MenuDeleteValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除菜单的路由依赖

    Args:
        delete_schema (MenuDeleteValidateSchema): 删除菜单所需的参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await MenuService.delete_menu(db_session, delete_schema)
